package com.chris.easymallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.chris.easymallproduct.dto.AttrGroupAddDto;
import com.chris.easymallproduct.dto.AttrGroupWithAttrsDto;
import com.chris.easymallproduct.dto.AttrRelationDto;
import com.chris.easymallproduct.entity.AttrEntity;
import com.chris.easymallproduct.vo.AttrGroupInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chris.easymallproduct.entity.AttrGroupEntity;
import com.chris.easymallproduct.service.AttrGroupService;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.R;


/**
 * 属性分组
 *
 * @author chris
 * @email wysiwyg7@163.com
 * @date 2022-08-14 19:42:02
 */
@RestController
@RequestMapping("easymallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{id}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("id") Long id) {
        PageUtils page = attrGroupService.queryPageById(params, id);

        return R.ok().put("page", page);
    }

    /**
     * 已关联基础属性列表
     */
    @RequestMapping("{id}/attr/relation")
    public R attrRelation(@PathVariable("id") Long id) {
        List<AttrEntity> data = attrGroupService.attrRelationInfo(id);

        return R.ok().put("data", data);
    }

    /**
     * 未关联基础属性列表
     */
    @RequestMapping("{id}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params, @PathVariable("id") Long id) {
        PageUtils page = attrGroupService.noattrRelation(params, id);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupInfoVo vo = attrGroupService.info(attrGroupId);
        return R.ok().put("attrGroup", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupAddDto attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性关系
     */
    @RequestMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrRelationDto[] deleteDtos) {
        attrGroupService.attrRelationDelete(deleteDtos);
        return R.ok();
    }

    /**
     * 绑定 基本属性和分组
     */
    @RequestMapping("/attr/relation")
    public R attrRelation(@RequestBody AttrRelationDto[] deleteDtos) {
        attrGroupService.attrRelation(deleteDtos);
        return R.ok();
    }

    /**
     * 根据分类Id获取属性组以及属性组里面的熟悉
     */
    @RequestMapping("/{catelogId}/withattr")
    public R attrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsDto> dtos = attrGroupService.attrGroupWithAttrs(catelogId);
        return R.ok().put("data", dtos);
    }


}
